﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TestConsoleApp
{
    public class TestParallel
    {
        static int N = 100;

        public static void Test()
        {
            int num = 0;
            object a = new object();

            Stopwatch sw = new Stopwatch();
            sw.Start();
            Parallel.For(0, N, i =>
            {
                Console.WriteLine("开启Task---" + Thread.CurrentThread.ManagedThreadId+"---date:"+DateTime.Now.ToString("mm:ss.fff"));

                lock (a)
                {
                    num++;
                }
                Thread.Sleep(1000);
            });
            sw.Stop();
            Console.WriteLine("Parallel---" + num + "---消耗时间：" + sw.ElapsedMilliseconds);

            num = 0;
            sw.Restart();
            for (int i = 0; i < N; i++)
            {
                num++;
                Thread.Sleep(1000);
            }
            sw.Stop();
            Console.WriteLine("for---" + num + "---消耗时间：" + sw.ElapsedMilliseconds);



        }
    }
}
